package com.jksoft.darewayyyy.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
import android.widget.Toast;

import com.jksoft.darewayyyy.global.MyApplication;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.HttpUtils;


/**
 * Created by Administrator on 2015/8/4.
 */
public class StrutsUtil {
    private static String processURL = "http://1.dwyyy.sinaapp.com/dwyyy/getPerInfo";

    public static Map urlPostRequest(String empId) throws Exception {
        String params = "userName=" + empId + "&uuid=" + MyApplication.UUID + "&major=" + MyApplication.MAJOR + "&minor=" + MyApplication.MINOR;
        byte[] data = params.getBytes();

        URL url = new URL(processURL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(3000);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", data.length + "");
        conn.setDoOutput(true);
        conn.getOutputStream().write(data);
        if (conn.getResponseCode() == 200) {
            BufferedReader r = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = null;
            StringBuffer sb = new StringBuffer();
            while ((line = r.readLine()) != null) {
                sb.append(line);
            }

            JSONObject jso = new JSONObject(sb.toString());
            System.out.println(jso);

            String errcode = jso.getString("errcode");

            Map map = null;
            System.out.println("errcode : " + errcode);
            if ("0".equals(errcode)) {
                String empdepart = jso.getString("empdepart");
                String empname = jso.getString("empname");
                String recId = jso.getString("recId");

                String pic = jso.getString("pic");

                pic = URLDecoder.decode(pic);

                byte[] picbytes = Base64.decode(pic, Base64.DEFAULT);

                Bitmap btm = BitmapFactory.decodeByteArray(picbytes, 0, picbytes.length);

                map = new HashMap();
                map.put("empname", empname);
                map.put("photo", btm);
                map.put("recId", recId);
                map.put("empdepart", empdepart);
            }

            return map;
        }
        return null;

    }

    public static void setConfirmEatFlag(String recId) throws Exception {
        recId = URLEncoder.encode(recId);
        String params = "recId=" + recId;
        byte[] data = params.getBytes();

        URL url = new URL("http://10.1.94.51:9090/dwyyy/setConfirmEatFlag");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(3000);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", data.length + "");

        conn.setDoOutput(true);
        conn.getOutputStream().write(data);
        if (conn.getResponseCode() == 200) {
            BufferedReader r = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = null;
            StringBuffer sb = new StringBuffer();
            while ((line = r.readLine()) != null) {
                sb.append(line);
            }

            JSONObject jso = new JSONObject(sb.toString());
            System.out.println(jso);

            String errcode = jso.getString("errcode");

            Map map = null;
            System.out.println("errcode : " + errcode + " ; " + "errtext : " + jso.getString("errtext"));
        }
    }

    /**
     *
     *
     * @param empId
     * @return
     * @throws Exception
     */
    public static Map postByScanQr(String empId) throws Exception {
        try {
            String scanUrl = "http://1.dwyyy.sinaapp.com/dwyyy/getScanInfo";
//            String scanUrl = "http://10.1.94.33:8080/dwyyy/getScanInfo";
            String params = "empId=" + empId + "&uuid=" + MyApplication.UUID + "&major=" + MyApplication.MAJOR + "&minor=" + MyApplication.MINOR;
            byte[] data = params.getBytes();

            URL url = new URL(scanUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(3000);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", data.length + "");

            conn.setDoOutput(true);
            conn.getOutputStream().write(data);
            if (conn.getResponseCode() == 200) {
                BufferedReader r = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line = null;
                StringBuffer sb = new StringBuffer();
                while ((line = r.readLine()) != null) {
                    sb.append(line);
                }
                JSONObject jso = new JSONObject(sb.toString());
                System.out.println(jso);

                String errcode = jso.getString("errcode");

                Map map = null;
                System.out.println("errcode : " + errcode);
                if ("0".equals(errcode)) {
                    String empdepart = jso.getString("empdepart");
                    String empname = jso.getString("empname");
                    String recId = jso.getString("recId");

                    String pic = jso.getString("pic");

                    pic = URLDecoder.decode(pic);

                    byte[] picbytes = Base64.decode(pic, Base64.DEFAULT);

                    Bitmap btm = BitmapFactory.decodeByteArray(picbytes, 0, picbytes.length);

                    map = new HashMap();
                    map.put("errcode", errcode);
                    map.put("empname", empname);
                    map.put("photo", btm);
                    map.put("recId", recId);
                    map.put("empdepart", empdepart);
                } else {
                    String errtext = jso.getString("errtext");
                    map = new HashMap();
                    map.put("errcode", errcode);
                    map.put("errtext", errtext);
                }

                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
